package cs.ui250.carnatic;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;

public class ShrutiBoxActivity extends Activity {

	Spinner mShrutiSpinner;
    MediaPlayer mp;
	AssetFileDescriptor afd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shruti_box);
		
		//Spinner
		mShrutiSpinner = (Spinner) findViewById(R.id.shruti_spinner);
		ArrayAdapter<CharSequence> sAdapter = ArrayAdapter.createFromResource(
				this, R.array.shrutis, android.R.layout.simple_spinner_item);
		sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mShrutiSpinner.setAdapter(sAdapter);
		
		//might move down there later
		
		mp = MediaPlayer.create(getBaseContext(), R.raw.shruti1);		

		
		mShrutiSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
		{
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
		    {
		    	switch (position)
		    	{
		    	case 0:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti1);
		    		break;
		    	case 1:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti1b);
		    		break;
		    	case 2:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti2);
		    		break;
		    	case 3:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti2b);
		    		break;
		    	case 4:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti3);
		    		break;
		    	case 5:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti4);
		    		break;
		    	case 6:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti4b);
		    		break;
		    	case 7:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti5);
		    		break;
		    	case 8:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti5b);
		    		break;
		    	case 9:
		    		afd = getBaseContext().getResources().openRawResourceFd(R.raw.shruti6);
		    		break;
		    	}
		    	
	    			mp.reset();
					try {
						mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
						mp.setLooping(true);
						mp.prepareAsync();
						afd.close();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

                mp.setOnCompletionListener(new OnCompletionListener()
                {
                    @Override
                    public void onCompletion(MediaPlayer mp)
                    {
                        mp.release();
                    }
                });
		    }
			@Override
			public void onNothingSelected(AdapterView<?> arg0){}
		});

        //ImageButton
		ImageButton mPausePlayButton = (ImageButton) findViewById(R.id.shruti_pause_play_button);
		mPausePlayButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if (mp.isPlaying())
				{
					mp.pause();
				}
				else
					mp.start();
			}
		});
	}

	@Override
	public void onAttachedToWindow()
	{
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
	@Override
	public void onStop()
	{
		super.onStop();
		if (mp.isPlaying())
		{
			mp.stop();
		}
	}
}